package org.dromara.tearoom.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.tearoom.domain.bo.TerIotDeviceOperLogBo;
import org.dromara.tearoom.domain.vo.TerIotDeviceOperLogVo;
import org.dromara.tearoom.service.ITerIotDeviceOperLogService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * IoT设备操作日志
 * 前端访问路由地址为:/tearoom/iotDeviceOperLog
 *
 * @author ckj
 * @date 2025-04-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iotDeviceOperLog")
public class TerIotDeviceOperLogController extends BaseController {

    private final ITerIotDeviceOperLogService terIotDeviceOperLogService;

    /**
     * 查询IoT设备操作日志列表
     */
    @SaCheckPermission("tearoom:iotDeviceOperLog:list")
    @GetMapping("/list")
    public TableDataInfo<TerIotDeviceOperLogVo> list(TerIotDeviceOperLogBo bo, PageQuery pageQuery) {
        return terIotDeviceOperLogService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出IoT设备操作日志列表
     */
    @SaCheckPermission("tearoom:iotDeviceOperLog:export")
    @Log(title = "IoT设备操作日志", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TerIotDeviceOperLogBo bo, HttpServletResponse response) {
        List<TerIotDeviceOperLogVo> list = terIotDeviceOperLogService.queryList(bo);
        ExcelUtil.exportExcel(list, "IoT设备操作日志", TerIotDeviceOperLogVo.class, response);
    }

    /**
     * 获取IoT设备操作日志详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tearoom:iotDeviceOperLog:query")
    @GetMapping("/{id}")
    public R<TerIotDeviceOperLogVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(terIotDeviceOperLogService.queryById(id));
    }

    /**
     * 新增IoT设备操作日志
     */
    @SaCheckPermission("tearoom:iotDeviceOperLog:add")
    @Log(title = "IoT设备操作日志", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TerIotDeviceOperLogBo bo) {
        return toAjax(terIotDeviceOperLogService.insertByBo(bo));
    }

    /**
     * 修改IoT设备操作日志
     */
    @SaCheckPermission("tearoom:iotDeviceOperLog:edit")
    @Log(title = "IoT设备操作日志", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TerIotDeviceOperLogBo bo) {
        return toAjax(terIotDeviceOperLogService.updateByBo(bo));
    }

    /**
     * 删除IoT设备操作日志
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tearoom:iotDeviceOperLog:remove")
    @Log(title = "IoT设备操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(terIotDeviceOperLogService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 删除IoT设备操作日志
     *
     * @param deviceKeys 设备编号
     */
    @SaCheckPermission("tearoom:iotDeviceOperLog:remove")
    @Log(title = "IoT设备操作日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/delAll/{deviceKeys}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("deviceKeys") String[] deviceKeys) {
        return toAjax(terIotDeviceOperLogService.deleteBatchByDeviceKeys(List.of(deviceKeys), true));
    }
}
